package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.module.bean.vo.MallAftersaleVO;
import com.cskaoyan.module.bo.AfterSaleId;
import com.cskaoyan.module.pojo.MarketAftersale;
import com.cskaoyan.module.pojo.MarketAftersaleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/6  22:16
 */
@Service
public class MallAfterSaleServiceImpl implements MallAfterSaleService {
    @Autowired
    MarketAftersaleMapper aftersaleMapper;


    //售后页面展示
    @Override
    public MallAftersaleVO listOrder(Integer page, Integer limit, String status, String sort, String order, String aftersaleSn, Integer orderId) {
        //分页设置
        PageHelper.startPage(page, limit);

        //创建一个example对象
        MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
        //使用这个createCriteria方法创建Example的内部类对象 → 里面维护了条件列表
        MarketAftersaleExample.Criteria criteria = marketAftersaleExample.createCriteria();
        //要返回的VO
        MallAftersaleVO aftersaleVO = new MallAftersaleVO();
        //分页插件获取返回数据
        PageInfo<MarketAftersale> pageInfo = null;


        List<MarketAftersale> marketAftersales1 = null;
        List<MarketAftersale> marketAftersales2 = null;
        List<MarketAftersale> marketAftersales3 = null;
        List<MarketAftersale> marketAftersales4 = null;


        if (status != null && !status.equals("")) {
            criteria.andStatusEqualTo(Short.valueOf(status));
            marketAftersales1 = aftersaleMapper.selectByExample(marketAftersaleExample);
            aftersaleVO.setList(marketAftersales1);
            pageInfo = new PageInfo<>(marketAftersales1);
        }
        if (aftersaleSn != null && !aftersaleSn.equals("")) {
            criteria.andAftersaleSnEqualTo(aftersaleSn);
            marketAftersales2 = aftersaleMapper.selectByExample(marketAftersaleExample);
            aftersaleVO.setList(marketAftersales2);
            pageInfo = new PageInfo<>(marketAftersales2);
        }
        if (orderId != null) {
            criteria.andOrderIdEqualTo(orderId);
            marketAftersales3 = aftersaleMapper.selectByExample(marketAftersaleExample);
            aftersaleVO.setList(marketAftersales3);
            pageInfo = new PageInfo<>(marketAftersales3);

        } else {
            marketAftersales4 = aftersaleMapper.selectByExample(marketAftersaleExample);
            aftersaleVO.setList(marketAftersales4);
            pageInfo = new PageInfo<>(marketAftersales4);
        }

        //根据order来排序
        marketAftersaleExample.setOrderByClause(sort + " " + order);

        //获取分页的数据
        int pageSize = pageInfo.getPageSize();
        long total = pageInfo.getTotal();

        //将分页的数据放进要传出的VO类中
        aftersaleVO.setPages(pageSize);
        aftersaleVO.setTotal((int) total);
        aftersaleVO.setPage(page);
        aftersaleVO.setLimit(limit);


        return aftersaleVO;


    }


    //售后单一通过
    @Override
    public void batchRecept(MarketAftersale marketAftersale) {

        aftersaleMapper.updateSingById(marketAftersale.getId());

    }

    //售后批量拒绝
    @Override
    public void batchRehect(AfterSaleId ids) {
        aftersaleMapper.updateByidReject(ids.getIds());
    }

    //售后批量通过
    @Override
    public void batchReceptMany(AfterSaleId id) {
        aftersaleMapper.updateByidRecept(id.getIds());
    }

    //售后单一拒绝
    @Override
    public void batchRejectSingle(MarketAftersale marketAftersale) {
        aftersaleMapper.updateRejectSingleById(marketAftersale.getId());
    }

    //订单退款
    @Override
    public void refund(MarketAftersale marketAftersale) {
        aftersaleMapper.updateCustomerRefund(marketAftersale.getId());
    }
}
